﻿using System.Web.UI;

namespace WebDiagnostics.Diagnosers.ServerInfo
{
    public class ServerInfoDiagnosis : Diagnosis
    {
        public string IISVersion { get; set; }

        public long? MaxAllocatedMemory { get; set; }

        public string Name { get; set; }

        public string OSVersion { get; set; }

        public long? PhysicallyAllocatedMemory { get; set; }

        public int? ThreadCount { get; set; }

        public long? UsedMemory { get; set; }


        public override void Render(System.IO.TextWriter writer)
        {
            var htmlWriter = new HtmlTextWriter(writer);

            htmlWriter.RenderHeader(2, "Server Info");

            htmlWriter.RenderBeginDiv("server-information");

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.P);
            htmlWriter.WriteLine("Name: {0}", Name);
            htmlWriter.RenderEndTag();

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.P);
            htmlWriter.WriteLine("OS Version: {0}", OSVersion);
            htmlWriter.RenderEndTag();

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.P);
            htmlWriter.WriteLine("IIS Version: {0}", IISVersion);
            htmlWriter.RenderEndTag();

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.P);
            htmlWriter.WriteLine("Current Thread Count: {0}", ThreadCount);
            htmlWriter.RenderEndTag();

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Fieldset, "memory-usage");

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table);

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Caption);
            htmlWriter.Write("Memory Usage");
            htmlWriter.RenderEndTag();

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlWriter.Write("Max Allocated");
                htmlWriter.RenderEndTag();
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlWriter.Write("{0:N0}K", MaxAllocatedMemory / 1024);
                htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlWriter.Write("Physical Allocated (current)");
                htmlWriter.RenderEndTag();
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlWriter.Write("{0:N0}K", PhysicallyAllocatedMemory / 1024);
                htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();

            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlWriter.Write("Used (current)");
                htmlWriter.RenderEndTag();
                htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
                htmlWriter.Write("{0:N0}K", UsedMemory / 1024);
                htmlWriter.RenderEndTag();
            htmlWriter.RenderEndTag();


            htmlWriter.RenderEndTag();

            htmlWriter.RenderEndTag();
        }
    }
}